﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.Office.Server.UserProfiles;
using System.Diagnostics;

namespace MMCP
{
    public static class UserProfileExtensions
    {
        //add you custom field names if you want to add them to claims provider
        public static readonly List<string> propList = new List<string>() { "SPS-Responsibility", "SPS-Skills", "SPS-Interests" };

        /// <summary>
        /// Retrieve UserProfile Taxonomy fields' values
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="propName"></param>
        /// <returns></returns>
        public static IEnumerable<Term> GetProfileFieldValues(this UserProfile profile, string propName)
        {
            List<Term> ret = new List<Term>();
            if (profile == null)
                return ret;

            var values = profile.GetProfileValueCollection(propName);
            if (values == null)
                return ret;

            var terms = values.GetTaxonomyTerms();
            var termspaths = from t in terms
                             where t.IsAvailableForTagging
                             select t;

            termspaths = termspaths.Distinct();
#if DEBUG
            foreach (var t in termspaths)
                Debug.WriteLine(string.Format("Profile: {0} Prop: {1} Term: {2}", profile.DisplayName, propName, t));
#endif
            return termspaths;
        }

        public static IEnumerable<Term> GetProfileFieldValues(this UserProfile profile, IEnumerable<string> propNames)
        {
            IEnumerable<Term> ret = new List<Term>();

            foreach (var prop in propNames)
            {
                IEnumerable<Term> values = profile.GetProfileFieldValues(prop);
                ret = ret.Concat(values);
            }
            ret = ret.Where(term => term != null).Distinct();
            return ret;
        }

        /// <summary>
        /// Retrieve an aggregated users' terms
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public static IEnumerable<Term> GetMetaMembershipTerms(this UserProfile profile)
        {
            return profile.GetProfileFieldValues(propList);
        }

        public static IEnumerable<TermHierarchyData> GetSocialTerms(this UserProfileManager profileMgr)
        {
            return from propName in propList
                   let propMgr = profileMgr.DefaultProfileSubtypeProperties
                   let prop = propMgr.GetPropertyByName(propName)
                   let coreProp = prop.CoreProperty
                   let termSet = coreProp.TermSet
                   let termSetDataList = termSet.GetTermHierarchyData(prop.DisplayName, string.Format("{0}{1}{2}", termSet.Id.ToString("N"), termSet.Name, prop.Name))
                   from data in termSetDataList
                   select data;
        }
    }
}
